//
//  File.swift
//  CViews
//
//  Created by xhl_dabaoMac on 2021/11/15.
//

import Foundation
/// 用来布局的位置参考
public enum CLocation {
    case left
    case center
    case right
}

///布局方法
public extension UIView{
    
    /// 导航布局单个view的方法
    /// - Parameters:
    ///   - location: view放在那个位置
    ///   - view: 要布局的view
    ///   - maxHeight: 最大高度
    ///   - minWidth: 最小宽度呀
    ///   - action: view的点击事件
    func layoutSingleView(location : CLocation = .left,
                          view : UIView,
                          sideSpace:CGFloat = 0,
                          maxHeight:CGFloat = 44,
                          minWidth:CGFloat = 44,
                          action : (()-> Void)? ) {
        addSubview(view)
        view.snp.makeConstraints { make in
            switch location {
            case .left:
                make.left.equalToSuperview().offset(sideSpace)
            case .center:
                make.centerX.equalToSuperview()
            case .right:
                make.right.equalToSuperview().offset(-sideSpace)
            }
            make.centerY.equalToSuperview()
            make.height.lessThanOrEqualTo(maxHeight)
            make.width.greaterThanOrEqualTo(minWidth)
         }
        guard let act = action else{return}
        view.addTapGesture { act() }
    }
    
    
    /// 水平方向多个View的布局
    /// - Parameters:
    ///   - startlocation: 布局开始方向
    ///   - views: 需要布局的views，传入数组
    ///   - sideSpace: 距离边的间距
    ///   - itemSpace: 每个item的间距
    ///   - maxHeight: 最大高度
    ///   - minWidth: 最小宽度呀
    ///   - equeWidth: 是否每个item等宽
    ///   - action: 每个item的点击事件
    /// - Returns: item的点击索引，left：从左到右依次变大，right：从右到左依次变大
    func layoutMultipleView(startlocation : CLocation,
                            views : [UIView],
                            sideSpace:CGFloat = 0,
                            itemSpace:CGFloat = 0,
                            maxHeight:CGFloat = 44,
                            minWidth:CGFloat = 44,
                            bottom:CGFloat = 0,
                            equeWidth:Bool = false,
                            action : @escaping (Int) -> () ) {
        
        var tempView : UIView? //用了记录循环的上一个view，方便布局约束

        for index in 0..<views.count {
            let subView = views[index]
            
            if subView.superview == nil {
                addSubview(subView)
            }
            
            subView.snp.makeConstraints { make in
                switch startlocation {
                case .left:
                    
                    if let temp = tempView  {
                        make.left.equalTo(temp.snp.right).offset(itemSpace)
                    }else{
                        make.left.equalToSuperview().offset(sideSpace)
                    }
                    
                case .right:
                    
                    if let temp = tempView  {
                        make.right.equalTo(temp.snp.left).offset(itemSpace)
                    }else{
                        make.right.equalToSuperview().offset(-sideSpace)
                    }
                    
                case .center:
                    if let temp = tempView  {
                        make.left.equalTo(temp.snp.right).offset(sideSpace)
                    }else{
                        make.left.equalToSuperview().offset(itemSpace)
                    }
                    //最后一个view设置右边间距，撑大父View
                    if subView == views.last {
                        make.right.equalToSuperview().offset(-sideSpace)
                    }
                }
                
                make.centerY.equalToSuperview().priority(333)
                make.bottom.equalToSuperview().offset(bottom).priority(666)
                make.height.equalTo(maxHeight)
                make.width.greaterThanOrEqualTo(minWidth)
                if equeWidth ,let view = tempView{ make.width.equalTo(view.snp.width) }
                
             }
            
            tempView = subView

            //添加事件
            subView.addTapGesture {
                action(index)
            }
        }
    }
    
    func layoutVerticalView(frontView:UIView,
                            layoutView:UIView,
                            top:CGFloat = 0,
                            left:CGFloat = 0,
                            right:CGFloat = 0,
                            height:CGFloat = 0) {
        
        if layoutView.superview == nil {
            addSubview(layoutView)
        }
        layoutView.snp.makeConstraints { make in
            make.top.equalTo(frontView.snp.bottom).offset(top)
            make.left.equalToSuperview().offset(left)
            make.right.equalToSuperview().offset(left)
            if height > 0{ make.height.equalTo(height)}
        }
    }
    
}



public extension UIScrollView {
    
    func scrollToCenter(tapSubView:UIView) {
        
        //未铺满，不做改变
        if contentSize.width <= bounds.width { return }
        
        var offsetX = tapSubView.center.x - self.bounds.size.width/2
        
        if offsetX < 0 {
            offsetX = 0
        }
        
        let maxRight = self.contentSize.width - self.bounds.size.width
        if offsetX > maxRight {
            offsetX = maxRight
        }
        self.contentOffset = CGPoint(x: offsetX, y: 0);
    }
    
}
